﻿function ShowModal(divName) {

    var backgroundElement = document.getElementById('divBackground');
    var foregroundElement = document.getElementById(divName);
    var mainTable = document.getElementById('mainTable');

    var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

    var clientWidth = mainTable.clientWidth; //document.documentElement.clientWidth;
    var clientHeight = mainTable.clientHeight; //document.documentElement.clientHeight;

    backgroundElement.style.display = 'block';
    foregroundElement.style.display = 'block';

    backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientWidth) + 'px';
    backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientHeight) + 'px';

    var foregroundelementwidth = foregroundElement.offsetWidth ? foregroundElement.offsetWidth : foregroundElement.scrollWidth;
    foregroundElement.style.left = ((clientWidth - foregroundelementwidth) / 2) + 'px';

    var foregroundelementheight = foregroundElement.offsetHeight ? foregroundElement.offsetHeight : foregroundElement.scrollHeight;
    foregroundElement.style.top = ((clientHeight - foregroundelementheight) / 2) + 'px';
}

function CloseModal(divName) {
    var backgroundElement = document.getElementById('divBackground');
    var foregroundElement = document.getElementById(divName);
    
    backgroundElement.style.display = 'none';
    foregroundElement.style.display = 'none';
}

function SetStyle(item)
{
    for (i = 0; i < item.parentNode.childNodes.length; i++)
    {
        if (item.parentNode.childNodes[i].className != 'grid-header' && item.parentNode.childNodes[i].className != '')
            item.parentNode.childNodes[i].className = 'rowgrid';
    }

    item.className = 'rowgridclick';
}

function SetItem(item, vision)
{
    SetStyle(item);
    
    switch (vision)
    {
        case '0':
            SetItemModule(item);
            break;
        case '1':
            SetItemSubModule(item);
            break;
        case '2':
            SetItemService(item);
            break;
        case '3':
            SetItemMethod(item);
            break;
        case '4':
            SetItemParameter(item);
            break;
        case '5':
            SetItemAttribute(item);
            break;
        case '6':
            SetItemAttribute(item);
            break;
        case '7':
            SetItemType(item);
            break;
        case '8':
            SetItemAttributePopup(item);
            break;
    };
}

function SetItemModule(item)
{
    document.getElementById('txtModuleIdEdit').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtModuleEdit').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtInitialsEdit').value = Trim(item.childNodes[2].innerText);
}

function SetItemSubModule(item)
{
    document.getElementById('txtSubModuleIdEdit').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtSubModuleEdit').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtPortEdit').value = Trim(item.childNodes[2].innerText);
}

function SetItemService(item)
{
    document.getElementById('txtServiceIdEdit').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtServiceEdit').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtServiceDescriptionEdit').value = Trim(item.childNodes[3].innerText);
    SetListBox(document.getElementById('lbEndPointEdit'), item.childNodes[4].innerText, ',');
    document.getElementById('ddlServiceApproval').value = Trim(item.childNodes[5].innerText) == "Aprovado" ? "0" : "1";
}

function SetItemMethod(item)
{
    document.getElementById('txtMethodIdEdit').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtMethodEdit').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtTypeEdit').value = Trim(item.childNodes[2].innerText);
    document.getElementById('txtDescriptionMethodEdit').value = Trim(item.childNodes[3].innerText);
    document.getElementById('txtRelatedTablesEdit').value = Trim(item.childNodes[4].innerText);
    document.getElementById('chkCollectionEdit').checked = item.childNodes[5].innerText == 'True' ? true : false;
    document.getElementById('ddlMethodApproval').value = Trim(item.childNodes[6].innerText) == "Aprovado" ? "0" : "1";
}

function SetItemParameter(item)
{
    document.getElementById('txtParameterIdEdit').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtParameterEdit').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtTypeParameterEdit').value = Trim(item.childNodes[2].innerText);
    document.getElementById('txtDescriptionParameterEdit').value = Trim(item.childNodes[3].innerText);
    document.getElementById('chkCollectionParameterEdit').checked = item.childNodes[4].innerText == 'True' ? true : false;
}

function SetItemType(item) {
    document.getElementById('txtTypeIdEditPopup').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtTypeEditPopup').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtTypeDescriptionEditPopup').value = Trim(item.childNodes[3].innerText);
}

function SetItemAttribute(item) {
    document.getElementById('txtAttributeIdEdit').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtAttributeEdit').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtTypeAttributeEdit').value = Trim(item.childNodes[2].innerText);
    document.getElementById('txtDescriptionAttributeEdit').value = Trim(item.childNodes[3].innerText);
}

function SetItemAttributePopup(item) {
    document.getElementById('txtAttributeIdEditPopup').value = Trim(item.childNodes[0].innerText);
    document.getElementById('txtAttributeEditPopup').value = Trim(item.childNodes[1].innerText);
    document.getElementById('txtTypeAttributeEditPopup').value = Trim(item.childNodes[2].innerText);
    document.getElementById('txtDescriptionAttributeEditPopup').value = Trim(item.childNodes[3].innerText);
}

function SetItemPopup(item) {
    var isValid = false;
    var txtTypeEdit = document.getElementById('txtTypeEdit');
    var txtTypeParameterEdit = document.getElementById('txtTypeParameterEdit');

    if (txtTypeEdit != null) {
        txtTypeEdit.value = item.childNodes[1].innerText;
        isValid = true;
    }

    if (txtTypeParameterEdit != null) {
        txtTypeParameterEdit.value = item.childNodes[1].innerText;
        isValid = true;
    }
    
    if (isValid)
        CloseModal('divType');
}

function Enter(vision)
{
    if (window.event.keyCode != 13)
        return
    
    switch (vision)
    {
        case '0':
            $get('btnSaveModule').click();
            break;
        case '1':
            $get('btnSaveSubModule').click();
            break;
        case '2':
            $get('btnSaveService').click();
            break;
        case '3':
            $get('btnSaveMethod').click();
            break;
        case '4':
            $get('btnSaveParameter').click();
            break;
        case '5':
            $get('btnSearch').click();
            break;
        case '6':
            $get('btnSaveAttribute').click();
            break;
        case '7':
            $get('btnSaveTypePopup').click();
            break;
        case '8':
            $get('btnSaveAttributePopup').click();
            break;
    };
    
    window.event.keyCode = 0;
}

function SetListBox(objListBox, separatedText, textSeparator) //inc - esertorio - 200811051000
{
    var textItens = new Array();
    textItens = separatedText.split(textSeparator);
    for(var j=0; j<objListBox.length; j++)
    {
        objListBox.options[j].selected = false;
        for(var i=0; i<textItens.length; i++)
        {
            //alert('textItens[' + i + ']=[' + textItens[i] + ']'); //debug
            if(objListBox.options[j].text == textItens[i])
            {
                //alert('objListBox.options['+j+'].text == textItens['+i+']'); //debug
                objListBox.options[j].selected = true;
                break;
            }
        }
    }
}

function Trim(str) {
    return str.replace(/^\s+|\s+$/g, ""); 
}
